/**
 * Copyright &copy; 2016 CNJSON All rights reserved.
 */
package com.hongsuan.pplive.modules.live.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.cnjson.db.persistence.Page;
import com.cnjson.common.utils.StringUtils;
import com.cnjson.common.web.BaseController;
import com.cnjson.common.config.Global;
import com.hongsuan.pplive.modules.live.entity.LiveBacklist;
import com.hongsuan.pplive.modules.live.service.LiveBacklistService;

/**
 * 单表生成Controller
 * @author cgli
 * @version 2016-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/live/liveBacklist")
public class LiveBacklistController extends BaseController {

	@Autowired
	private LiveBacklistService liveBacklistService;

	@ModelAttribute
	public LiveBacklist get(@RequestParam(required=false) String id) {
		LiveBacklist entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = liveBacklistService.get(id);
		}
		if (entity == null){
			entity = new LiveBacklist();
		}
		return entity;
	}

	@RequiresPermissions("live:liveBacklist:view")
	@RequestMapping(value = {"list", ""})
	public String list(LiveBacklist liveBacklist, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<LiveBacklist> page = liveBacklistService.findPage(new Page<LiveBacklist>(request, response), liveBacklist);
		model.addAttribute("page", page);
		return "modules/live/liveBacklistList";
	}

	@RequiresPermissions("live:liveBacklist:view")
	@RequestMapping(value = "form")
	public String form(LiveBacklist liveBacklist, Model model) {
		model.addAttribute("liveBacklist", liveBacklist);
		return "modules/live/liveBacklistForm";
	}

	@RequiresPermissions("live:liveBacklist:edit")
	@RequestMapping(value = "save")
	public String save(LiveBacklist liveBacklist, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, liveBacklist)){
			return form(liveBacklist, model);
		}
		liveBacklistService.save(liveBacklist);
		addMessage(redirectAttributes, "保存单表成功");
		return "redirect:"+Global.getAdminPath()+"/live/liveBacklist/?repage";
	}

	@RequiresPermissions("live:liveBacklist:edit")
	@RequestMapping(value = "delete")
	public String delete(LiveBacklist liveBacklist, RedirectAttributes redirectAttributes) {
		liveBacklistService.delete(liveBacklist);
		addMessage(redirectAttributes, "删除单表成功");
		return "redirect:"+Global.getAdminPath()+"/live/liveBacklist/?repage";
	}

}